package test;

import com.itstyle.quartz.kettleUtil.FileReadUtil;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author ycj
 * @version 1.0
 * @description
 * @date 2020/10/22 15:14
 */
public class Thread2 implements Runnable {
    private static int a = 10000;
    private static Integer b = 0;
    private static ArrayList<String> list = null;

    public static void main(String[] args) {
        start();
//        read();
    }

    public static void start() {
        Thread2 thread2 = new Thread2();
        for (int i = 0; i <10; i++) {
            Thread thread1 = new Thread(thread2);
            thread1.start();
        }
    }

    @Override
    public void run() {
        long starttime = System.currentTimeMillis();

        File[] file = FileReadUtil.getFile("D:\\dianli\\工作材料\\各类文件");
        File file1 = new File("D:\\dianli\\工作材料\\k\\1.txt");
        String path = null;

        HashMap<String, List<String>> read1 = FileReadUtil.read(path);
        for (String s : read1.keySet()) {
            list.add(s);
        }




        while (true) {
            synchronized (this) {
                if (b < file.length) {
                    try {
                        Thread.sleep(3000);
                        path = file[b].getPath();
                        HashMap<String, List<String>> read = FileReadUtil.read(path);
                        FileOutputStream stream = null;
                        OutputStreamWriter writer = null;
                        try {
                            stream = new FileOutputStream(file1);
                            List<String> value = read.get("value");
                            for (String s : value) {
                                stream.write(s.getBytes("UTF-8"));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }finally {
                            try {
                                stream.close();

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    if (b < file.length) {
                        System.out.println(Thread.currentThread().getName() + "------" + path);
                        b++;
                    } else {
                        break;
                    }
                }else {
                    break;
                }
            }
        }
        long endtime = System.currentTimeMillis();
        System.out.println("程序执行时间"+(endtime-starttime));
    }

    public static void read() {
        long starttime = System.currentTimeMillis();
        File file1 = new File("D:\\dianli\\工作材料\\k\\1.txt");
//        for (int i = 0; i <10000; i++) {
//            System.out.print(Thread.currentThread().getName()+"-----------"+i);
//            int a = 0;
//        }

        File[] file = FileReadUtil.getFile("D:\\dianli\\工作材料\\各类文件");
        for (int i = 0; i < file.length; i++) {
            String path = file[i].getPath();
            HashMap<String, List<String>> read = FileReadUtil.read(path);
            FileOutputStream stream = null;
            OutputStreamWriter writer = null;
            try {
                stream = new FileOutputStream(file1);
//                            writer = new OutputStreamWriter(stream,"UTF-8");
                List<String> value = read.get("value");
                for (String s : value) {
                    stream.write(s.getBytes("UTF-8"));
                }

            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    stream.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "------" + path);
        }

        long endtime = System.currentTimeMillis();
        System.out.println("程序执行时间"+(endtime-starttime));
    }

    public boolean readPath(String path) {

        for (String s : list) {
            if (path.equals(s)) {
                return true;
            }
        }
        return false;
    }
}
